﻿using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Media.Imaging;
using Cirrious.CrossCore;
using Cirrious.MvvmCross.Plugins.File;

namespace Bulb.UI.WindowsPhone.ValueConverters
{
    public class FilePathToImageValueConverter: IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return null;

            if (!(value is string))
                return null;

            var fileStore = Mvx.Resolve<IMvxFileStore>();

            var bitmap = new BitmapImage();
            fileStore.TryReadBinaryFile((string) value, (stream =>
                {
                    bitmap.SetSource(stream);
                    return true;
                }
            ));

            return bitmap;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
